package com.xxx.mch.controller;

import com.xxx.mch.domain.po.DepartmentReward;
import com.xxx.mch.domain.request.PersonalAwardRequest;
import com.xxx.mch.service.AwardService;
import com.xxx.mch.service.TotalScoreService;
import com.xxx.mch.service.impl.DepartmentServiceImpl;
import com.xxx.mch.utils.WrapMapper;
import com.xxx.mch.utils.Wrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author FengHaixin
 * @description 一句话描述该文件的用途
 * @date 2020/9/7
 */
@Slf4j
@RestController
@RequestMapping ("award")
@Api(tags = "奖金配置接口")
public class AwardController
{

    @Autowired
    private AwardService awardService;

    @Autowired
    private DepartmentServiceImpl departmentService;

    @Resource
    private TotalScoreService totalScoreService;


    @ApiOperation (value = "保存个人奖金系数。尤其注意：保存部门的奖金配置是原先的另外的接口")
    @PostMapping (value = "personal", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> saveAwardMode(@RequestBody @Valid PersonalAwardRequest awardModeRequest)
    {
        awardService.saveAwardMode(awardModeRequest);
        return WrapMapper.ok();
    }


    @ApiOperation (value = "查询个人奖金系数。尤其注意：查询部门的奖金配置是原先的另外的接口")
    @GetMapping (value = "personal", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<PersonalAwardRequest> queryAwardMode(@RequestParam("month") String month)
    {
        PersonalAwardRequest awardResponse = awardService.queryAwardMode(month);
        return WrapMapper.ok(awardResponse);
    }

    @ApiOperation (value = "确认奖金，确认后不可修改")
    @PostMapping (value = "confirm", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> confirm(@RequestParam("month") String month,
                                   @ApiParam(name = "type", value = "1：部门奖金；2：个人奖金", required = true) @RequestParam("type") Integer type)
    {
        awardService.confirm(month, type);
        return WrapMapper.ok();
    }

    @ApiOperation (value = "奖金确认状态查询")
    @GetMapping (value = "confirmStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<Boolean> confirmStatus(@RequestParam("month") String month)
    {
        return WrapMapper.ok(awardService.isConfirm(month));
    }

    @ApiOperation (value = "计算奖金")
    @GetMapping (value = "calculate", produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> calculate(@RequestParam("month") String month,
                                   @ApiParam(name = "type", value = "1：部门奖金；2：个人奖金", required = true) @RequestParam("type") Integer type)
    {
        totalScoreService.calculateAward(month, type);
        return WrapMapper.ok();
    }

    @ApiOperation (value = "查询部门奖金及系数")
    @GetMapping (produces = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<List<DepartmentReward>> getList(@RequestParam("month")String month)
    {
        return WrapMapper.ok(departmentService.getDepartmentReward(month));
    }

    @ApiOperation (value = "保存部门奖金及系数")
    @PostMapping (consumes = MediaType.APPLICATION_JSON_VALUE)
    public Wrapper<String> update(@RequestBody List<DepartmentReward> departmentRewardList)
    {
        departmentService.insertOrUpdateReward(departmentRewardList);
        return WrapMapper.ok();
    }
}
